package cn.kgc.springboot.vue.service.impl;

import cn.kgc.springboot.vue.commons.QueryCondition;
import cn.kgc.springboot.vue.response.ResponseResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.springboot.vue.entity.Course;
import cn.kgc.springboot.vue.service.CourseService;
import cn.kgc.springboot.vue.mapper.CourseMapper;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
* @author mengshun
* @description 针对表【sys_course】的数据库操作Service实现
* @createDate 2024-12-27 13:41:50
*/
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService{


    @Override
    public ResponseResult coursePage(QueryCondition<Course> queryCondition) {

        Page<Course> coursePage = new Page<>(queryCondition.getPage(), queryCondition.getLimit());
        String courseName = queryCondition.getSearchParams().getCourseName();

        LambdaQueryWrapper<Course> lambda = new QueryWrapper<Course>().lambda();
        lambda.like(StringUtils.isNotBlank(courseName),Course::getCourseName,courseName)
                .orderByDesc(Course::getCreateTime);
        baseMapper.selectPage(coursePage,lambda);

        return ResponseResult.success().data("total",coursePage.getTotal())
                .data("tableData",coursePage.getRecords());
    }

    @Override
    public ResponseResult deleteCoure(Integer id) {

        LambdaUpdateWrapper<Course> lambda = new UpdateWrapper<Course>().lambda();
        lambda.set(Course::getIsDelete,1)
                .eq(Course::getId,id);
        baseMapper.update(lambda);

        return ResponseResult.success();
    }

    @Override
    public ResponseResult changeState(HashMap<String, Integer> params) {

        Integer id = params.get("id");
        Integer isDelete = params.get("isDelete");

        LambdaUpdateWrapper<Course> lambda = new UpdateWrapper<Course>().lambda();
        lambda.set(Course::getIsDelete,isDelete)
                .eq(Course::getId,id);
        baseMapper.update(lambda);
        return ResponseResult.success();
    }
}




